﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel;
using System.Windows;
using System.Windows.Media;
using System.Windows.Input;
using System.Windows.Controls;
using System.Windows.Shapes;

namespace Shrestha.Trigraph
{
    class TriMarker : Shape
    {
        private TriEvent te;
        private MainWindow baseWindow;

        public double MarkerHeight, MarkerWidth, X, Y;

        public TriMarker(double xcoordinate, double ycoordinate, double height, double width, TriEvent e, MainWindow m)
        {
            this.Stroke = Brushes.Blue;
            this.Fill = Brushes.LightBlue;
            X = xcoordinate; Y = ycoordinate; MarkerHeight = height; MarkerWidth = width; te = e; baseWindow = m;
            string descriptionText = "Latitude: " + te.Latitude + ", Longitude: " + te.Longitude + "\nDate of Event: " + te.EventDate;
            this.ToolTip = descriptionText;
        }

        public TriMarker() { }

        protected override Geometry DefiningGeometry
        {
            get
            {
                // Create a StreamGeometry for describing the shape
                StreamGeometry geometry = new StreamGeometry();
                geometry.FillRule = FillRule.EvenOdd;

                using (StreamGeometryContext context = geometry.Open())
                {
                    InternalTriMarkerGeometry(context);
                }

                // Freeze the geometry for performance benefits
                geometry.Freeze();

                return geometry;
            }
        }

        private void InternalTriMarkerGeometry(StreamGeometryContext context)
        {
            int mh = (int)(MarkerHeight / 2);
            int mw = (int)(MarkerWidth / 2);
            Point p1 = new Point(X-mw, Y-mh);
            Point p2 = new Point(X + mw, Y-mh);
            Point p3 = new Point(X + mw, Y + mh);
            Point p4 = new Point(X-mw, Y + mh);

            context.BeginFigure(p1, true, true);
            context.LineTo(p2, true, true);
            context.LineTo(p3, true, true);
            context.LineTo(p4, true, true);
        }

        protected override void OnMouseDown(MouseButtonEventArgs e)
        {
            string descriptionText = "Latitude: " + te.Latitude + ", Longitude: " + te.Longitude + "\nDate of Event: " + te.EventDate;
            baseWindow.descriptionBlock.Text = descriptionText;

            this.Stroke = Brushes.Red;
            this.Fill = Brushes.PeachPuff;
            
            base.OnMouseDown(e);
        }
    }
}
